#!/bin/sh
# add nbd for config
#make by yuanxihua@21cn.com  for 9G9260 
#make at91sam9260ek_defconfig
ROOTFS="rootfs"
KERNEL_FULL_VERSION="2.6.32-zlb9260"

function die()
{
    echo "fail:  $@"
    exit 1
}

if [ ! -d ${ROOTFS}/tftpboot ] ; then
    mkdir -pv ${ROOTFS}/tftpboot
fi
if [ ! -d ${ROOTFS}/nfsboot/lib/modules ] ; then
    mkdir -pv ${ROOTFS}/nfsboot/lib/modules
fi

if [ ! -d ${ROOTFS}/nfsboot/rootfs/lib/modules ] ; then
    mkdir -pv ${ROOTFS}/nfsboot/rootfs/lib/modules 
fi


#make CROSS_COMPILE=arm-linux-  ARCH=arm
#make CROSS_COMPILE=arm-linux-  ARCH=arm uImage

make CROSS_COMPILE=armv5tl-softfloat-linux-gnueabi- ARCH=arm  || die "make kernel"
make CROSS_COMPILE=armv5tl-softfloat-linux-gnueabi- ARCH=arm uImage || die "make uImage"


cp arch/arm/boot/uImage ${ROOTFS}/uImage-${KERNEL_FULL_VERSION}.bin  || die "cp uImage fail"
sudo cp ${ROOTFS}/uImage-${KERNEL_FULL_VERSION}.bin /var/tftpboot/  || die "cp rootfs tftpboot"
cp ${ROOTFS}/uImage-${KERNEL_FULL_VERSION}.bin ${ROOTFS}/tftpboot ||  die "cp rootfs ${ROOTFS}/tftpboot"
make modules_install INSTALL_MOD_PATH=${ROOTFS}/modules-${KERNEL_FULL_VERSION}
#tar czvf modules-${KERNEL_FULL_VERSION}.tar.gz ${ROOTFS}/modules-${KERNEL_FULL_VERSION}
#sudo ./depmod.pl -k ./vmlinux -b ./rootfs-zlb9260_armv5tl/lib/modules > ./rootfs-zlb9260_armv5tl/lib/modules/${KERNEL_FULL_VERSION}/modules.dep
sudo ./depmod.pl -k ./vmlinux -b ${ROOTFS}/nfsboot/rootfs/lib/modules/${KERNEL_FULL_VERSION} > ${ROOTFS}/nfsboot/rootfs/lib/modules/${KERNEL_FULL_VERSION}/modules.dep || die "depmod.pl "
#rootfs/nfsboot/rootfs/lib/modules/${KERNEL_FULL_VERSION}
if [ -d /home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/modules/${KERNEL_FULL_VERSION} ] ; then
    sudo rm -rf  /home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/modules/${KERNEL_FULL_VERSION}
fi

sudo cp -a ${ROOTFS}/nfsboot/rootfs/lib/modules/${KERNEL_FULL_VERSION} /home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/modules/ || die "cp modules fail"


#tar xzvf modules-2.6.32-zlb9260.tar.gz -C /home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/
#/home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/modules/modules-2.6.32-zlb9260
#tar xzvf modules-2.6.32-zlb9260.tar.gz -C /home/lvdata/develop/9260/zlb_software/rootfs_dir/rootfs-zlb9260_armv5tl/lib/2.6.32-zlb9260
cp modules-${KERNEL_FULL_VERSION}.tar.gz  ${ROOTFS}/tftpboot
rm -rf ${ROOTFS}/nfsboot/lib/modules
cp -av ${ROOTFS}/modules-${KERNEL_FULL_VERSION}/lib/modules/* ${ROOTFS}/nfsboot/rootfs/lib/modules/ 
rm -rf ${ROOTFS}/modules-${KERNEL_FULL_VERSION}


#   cp arch/arm/boot/uImage ${ROOTFS}/uImage-2.6.32-9g9260.bin || echo "!!!!!"
#   cp uImage-2.6.32-9g9260.bin ${ROOTFS}/tftpboot
#   make modules_install INSTALL_MOD_PATH=${ROOTFS}/modules-2.6.32-9g9260
#   tar czvf modules-2.6.32-9g9260.tar.gz ${ROOTFS}/modules-2.6.32-9g9260
#   cp modules-2.6.32-9g9260.tar.gz  ${ROOTFS}/tftpboot
#   rm -rf ${ROOTFS}/nfsboot/lib/modules
#   cp -av ${ROOTFS}/modules-2.6.32-9g9260/lib/modules/* ${ROOTFS}/nfsboot/rootfs/lib/modules/ 
#   rm -rf ${ROOTFS}/modules-2.6.32-9g9260


